import java.util.Arrays;
import java.util.Scanner;
class Student implements Comparable<Student> {
    public int score;
    public char team;
    public int finScore;
    public int compareTo(Student s){
        if(s.finScore==this.finScore){
            return -(s.team-this.team);
        }
        return -(this.finScore-s.finScore);
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int k=sc.nextInt();
        Student[] s=new Student[n];
        int[][] arrScore=new int[k][k];
        int[] num=new int[k];
        for(int i=0;i<n;i++){
            Student s1=new Student();
            s1.score=sc.nextInt();
            s1.team=sc.next().charAt(0);
            s[i]=s1;
        }
        for(int i=0;i<k;i++){
            for(int j=0;j<k;j++){
                arrScore[i][j]=sc.nextInt();
            }
        }
        for(int j=0;j<k;j++){
            int sum=0;
            for(int i=0;i<k;i++){
                sum+=arrScore[i][j];
            }
            double ave1=sum*1.0/k;
            int sum2=0;
            int cnt=0;
            for(int i=0;i<k;i++){
                if(arrScore[i][j]+15>=ave1&&arrScore[i][j]-15<=ave1){
                    sum2+=arrScore[i][j];
                    cnt++;
                }
            }
            double ave2=sum2*1.0/cnt;
            num[j]= (int) Math.round(ave2);
        }
        for(int i=0;i<n;i++){
            double x=0.6*s[i].score+0.4*num[s[i].team-'A'];
            s[i].finScore= (int) Math.round(x);
        }
        Arrays.sort(s);
        for(int i=0;i<n;i++){
            System.out.println(s[i].finScore+" "+s[i].team);
        }
    }
}